﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;

namespace JaikuClient
{
    public partial class NetworkSettingsForm : Form
    {
        public NetworkSettingsForm()
        {
            InitializeComponent();
        }

        private void updateProxyAddressPortFormStatus(object sender, EventArgs e)
        {
            Boolean isChecked = useProxy.Checked;
            Boolean isAuthChecked = ProxyRequireAuth.Checked;
            if(isChecked){
                
                ProxyAddress.BackColor = Color.White;
                ProxyAddress.Enabled = true;
                ProxyPort.BackColor = Color.White;
                ProxyPort.Enabled = true;
                label2.ForeColor = Color.Black;
                label3.ForeColor = Color.Black;
                ProxyRequireAuth.ForeColor = Color.Black;
                ProxyRequireAuth.Enabled = true;
                if (isAuthChecked)
                {
                    ProxyUser.Enabled = true;
                    ProxyUser.BackColor = Color.White;
                    ProxyPassword.Enabled = true;
                    ProxyPassword.BackColor = Color.White;
                    label4.ForeColor = Color.Black;
                    label5.ForeColor = Color.Black;
                }

            }
                else{
                    ProxyAddress.Enabled = false;
                    ProxyPort.Enabled = false;
                    ProxyAddress.BackColor = Color.Gainsboro;
                    ProxyPort.BackColor = Color.Gainsboro;
                    label2.ForeColor = Color.Gray;
                    label3.ForeColor = Color.Gray;
                    ProxyRequireAuth.ForeColor = Color.Gray;
                    ProxyRequireAuth.Enabled = false;
                    ProxyUser.Enabled = false;
                    ProxyUser.BackColor = Color.Gainsboro;
                    ProxyPassword.Enabled = false;
                    ProxyPassword.BackColor = Color.Gainsboro;
                    label4.ForeColor = Color.Gray;
                    label5.ForeColor = Color.Gray;
                }
        }

        private void updateProxyAuthFormStatus(object sender, EventArgs e)
        {
            Boolean isChecked = ProxyRequireAuth.Checked;
            if (isChecked)
            {
                ProxyUser.Enabled = true;
                ProxyUser.BackColor = Color.White;
                ProxyPassword.Enabled = true;
                ProxyPassword.BackColor = Color.White;
                label4.ForeColor = Color.Black;
                label5.ForeColor = Color.Black;
            }
            else
            {
                ProxyUser.Enabled = false;
                ProxyUser.BackColor = Color.Gainsboro;
                ProxyPassword.Enabled = false;
                ProxyPassword.BackColor = Color.Gainsboro;
                label4.ForeColor = Color.Gray;
                label5.ForeColor = Color.Gray;
            }
        }

        private void SaveNetworkSettings(object sender, EventArgs e)
        {
            String UseProxyStr = "0";
            String UseProxyAuthStr = "0";
            Boolean UseProxy = useProxy.Checked;
            Boolean UseProxyAuth = ProxyRequireAuth.Checked;
            if(UseProxy) UseProxyStr = "1";
            if(UseProxyAuth) UseProxyAuthStr = "1";
            String proxyAddress = ProxyAddress.Text.ToString();
            String proxyPort = ProxyPort.Text.ToString();
            String proxyUser = ProxyUser.Text.ToString();
            String proxyPassword = ProxyPassword.Text.ToString();
            RegistryKey rk = Registry.CurrentUser;
            RegistryKey myrk = rk.CreateSubKey("SOFTWARE\\s0rs networks\\Jaiku Client\\HTTP Proxy");
            myrk.SetValue("USE_PROXY", UseProxyStr);
            myrk.SetValue("PROXY_ADDRESS", proxyAddress.ToString());
            myrk.SetValue("PROXY_PORT", proxyPort.ToString());
            myrk.SetValue("USE_PROXY_AUTH", UseProxyAuthStr);
            myrk.SetValue("PROXY_USER", proxyUser.ToString());
            myrk.SetValue("PROXY_PASSWORD", proxyPassword.ToString());
            this.Dispose();
            this.Hide();
        }

        private void LoadSettings(object sender, EventArgs e)
        {
            
            
            RegistryKey rk = Registry.CurrentUser;
            RegistryKey myrk = rk.CreateSubKey("SOFTWARE\\s0rs networks\\Jaiku Client\\HTTP Proxy");
            String UseProxyStr = "0";
            String UseProxyAuthStr = "0";
            UseProxyStr = (string)myrk.GetValue("USE_PROXY");
            UseProxyAuthStr = (string)myrk.GetValue("USE_PROXY_AUTH");
            String proxyAddress = (string)myrk.GetValue("PROXY_ADDRESS");
            String proxyPort = (string)myrk.GetValue("PROXY_PORT");
            String proxyUser = (string)myrk.GetValue("PROXY_USER");
            String proxyPassword = (string)myrk.GetValue("PROXY_PASSWORD");
            if (UseProxyStr == "1") { useProxy.Checked = true; } else { useProxy.Checked = false; }
            if (UseProxyAuthStr == "1") { ProxyRequireAuth.Checked = true; } else { ProxyRequireAuth.Checked = false; }
            ProxyUser.Text = proxyUser;
            ProxyPassword.Text = proxyPassword;
            ProxyAddress.Text = proxyAddress;
            ProxyPort.Text = proxyPort;
            updateFormStatus();

        }
        private void updateFormStatus()
        {
            Boolean isChecked = useProxy.Checked;
            Boolean isAuthChecked = ProxyRequireAuth.Checked;
            if (isChecked)
            {

                ProxyAddress.BackColor = Color.White;
                ProxyAddress.Enabled = true;
                ProxyPort.BackColor = Color.White;
                ProxyPort.Enabled = true;
                label2.ForeColor = Color.Black;
                label3.ForeColor = Color.Black;
                ProxyRequireAuth.ForeColor = Color.Black;
                ProxyRequireAuth.Enabled = true;
                if (isAuthChecked && isChecked)
                {
                    ProxyUser.Enabled = true;
                    ProxyUser.BackColor = Color.White;
                    ProxyPassword.Enabled = true;
                    ProxyPassword.BackColor = Color.White;
                    label4.ForeColor = Color.Black;
                    label5.ForeColor = Color.Black;
                }
                
            }
            if (isAuthChecked && !isChecked)
            {
                ProxyUser.Enabled = false;
                ProxyUser.BackColor = Color.Gainsboro;
                ProxyPassword.Enabled = false;
                ProxyPassword.BackColor = Color.Gainsboro;
                label4.ForeColor = Color.Gray;
                label5.ForeColor = Color.Gray;
            }

        }
    }
}
